package com.sen.paydemo.controller;


import com.sen.paydemo.enums.OrderStatus;
import com.sen.paydemo.service.OrderInfoService;
import com.sen.paydemo.entity.OrderInfo;
import com.sen.paydemo.param.OrderInfoParam;

import com.sen.paydemo.vo.ApiResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单信息控制器
 *
 * @author 韩宗森
 * @since 2024-01-09 19:53:30
 */
@Tag(name = "订单信息管理")
@RestController
@RequestMapping("/api/paydemo/order-info")
public class OrderInfoController extends BaseController {
    @Resource
    private OrderInfoService orderInfoService;


    @Operation(summary = "根据orderNo查询本地订单状态")
    @GetMapping("query-order-status/{orderNo}")
    public ApiResult<?> queryOrderStatus(@PathVariable("orderNo") String orderNo) {
        String orderStatus = orderInfoService.getOrderStatus(orderNo);
        if (OrderStatus.SUCCESS.getType().equals(orderStatus)) {
            return success("支付成功!");
        }

        ApiResult apiResult = new ApiResult<>();
        apiResult.setCode(101);
        apiResult.setMessage("支付中...");
        return apiResult;
    }


    @Operation(summary = "分页查询订单信息")
    @GetMapping("/page")
    public ApiResult<List<OrderInfo>> page(OrderInfoParam param) {

        return success(orderInfoService.list());
    }


    @Operation(summary = "查询全部订单信息")
    @GetMapping()
    public ApiResult<List<OrderInfo>> list(OrderInfoParam param) {

        return success(orderInfoService.list());

    }


    @Operation(summary = "根据id查询订单信息")
    @GetMapping("/{id}")
    public ApiResult<OrderInfo> get(@PathVariable("id") Integer id) {
        return success(orderInfoService.getById(id));

    }


    @Operation(summary = "添加订单信息")
    @PostMapping()
    public ApiResult<?> save(@RequestBody OrderInfo orderInfo) {
        if (orderInfoService.save(orderInfo)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }


    @Operation(summary = "修改订单信息")
    @PutMapping()
    public ApiResult<?> update(@RequestBody OrderInfo orderInfo) {
        if (orderInfoService.updateById(orderInfo)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }


    @Operation(summary = "删除订单信息")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (orderInfoService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }


    @Operation(summary = "批量添加订单信息")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<OrderInfo> list) {
        if (orderInfoService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }


    @Operation(summary = "批量删除订单信息")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (orderInfoService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
